package com.mysteel.cyx.demo.utils;


import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.Locale;


/**
 * 基于1.8的LocalDateTime实现的时间处理工具类
 *
 * @Author：cyx
 * @Date：2024/4/2 9:04
 */
public final class DateToolUtils {


    /**
     * LocalDateTime转long类型
     */
    public static long localDateTimeToEpochMilli(LocalDateTime localDateTime) {
        return localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }

    /**
     * long转LocalDateTime类型
     */
    public static LocalDateTime epochMilliToLocalDateTime(long epochMilli) {
        return LocalDateTime.ofInstant(Instant.ofEpochMilli(epochMilli), ZoneId.systemDefault());
    }


    /**
     * localDateTime转localDate
     *
     * @param localDateTime
     * @return
     */
    public static LocalDate localDateTimeToLocalDate(LocalDateTime localDateTime) {
        return localDateTime.toLocalDate();
    }

    /**
     * localDateTime转Date
     *
     * @param localDateTime
     * @return
     */
    public static Date localDateTimeToDate(LocalDateTime localDateTime) {
        //获取系统默认时区
        ZoneId zoneId = ZoneId.systemDefault();
        //时区的日期和时间
        ZonedDateTime zonedDateTime = localDateTime.atZone(zoneId);
        //获取时刻
        return Date.from(zonedDateTime.toInstant());
    }


    /**
     * localDate转localDateTime
     *
     * @param localDate
     * @return
     */
    public static LocalDateTime localDateToLocalDateTime(LocalDate localDate) {
        ZoneId zoneId = ZoneId.systemDefault();
        return localDate.atTime(LocalTime.now(zoneId));
    }

    /**
     * localDate转Date
     *
     * @param localDate
     * @return
     */
    public static Date localDateToDate(LocalDate localDate) {
        ZoneId zoneId = ZoneId.systemDefault();
        return Date.from(localDate.atStartOfDay().atZone(zoneId).toInstant());
    }

    /**
     * Date转localDate
     *
     * @param date
     * @return
     */
    public static LocalDate dateToLocalDate(Date date) {
        ZoneId zoneId = ZoneId.systemDefault();
        return date.toInstant().atZone(zoneId).toLocalDate();
    }

    /**
     * Date转LocalDateTime
     *
     * @param date
     * @return
     */
    public static LocalDateTime dateToLocalDateTime(Date date) {
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
    }


    /**
     * LocalDateTime转int类型
     * 输出格式:yyyyMMdd
     */
    public static int formatLocalDateTimeToPureDate(LocalDateTime localDateTime) {
        String format = localDateTime.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
        return Integer.parseInt(format);

    }


    /**
     * String转LocalDateTime类型
     * 输入格式:yyyyMMdd
     */
    public static LocalDateTime parseLocalDateTimeToPureDate(String localDateTime) {
        return LocalDateTime.parse(localDateTime, DateTimeFormatter.ofPattern("yyyyMMdd"));

    }

    /**
     * int转LocalDateTime类型
     * 输入格式:yyyyMMdd
     */
    public static LocalDateTime parseLocalDateTimeToPureDate(Integer localDateTime) {
        return parseLocalDateTimeToPureDate(String.valueOf(localDateTime));
    }

    /**
     * 获取指定时间戳偏移多少天的最后时间
     */
    public static Long endTimeOfOffsetDay(LocalDateTime localDateTime, long offsetDay) {
        LocalDateTime offsetLocalDateTime = localDateTime.plusDays(offsetDay);
        return localDateTimeToEpochMilli(offsetLocalDateTime.with(LocalTime.MAX));
    }


    /**
     * 获取指定时间戳偏移多少天的开始时间
     */
    public static Long startTimeOfOffsetDay(LocalDateTime localDateTime, long offsetDay) {
        LocalDateTime offsetLocalDateTime = localDateTime.plusDays(offsetDay);
        return localDateTimeToEpochMilli(offsetLocalDateTime.with(LocalTime.MIN));
    }


    /**
     * 获取指定时间偏移多少天的开始时间
     * 输入格式:yyyyMMdd
     */
    public static Long startTimeOfOffsetDay(int day, int offsetDay) {
        LocalDateTime localDateTime = parseLocalDateTimeToPureDate(day);
        LocalDateTime offsetLocalDateTime = localDateTime.plusDays(offsetDay);
        return localDateTimeToEpochMilli(offsetLocalDateTime.with(LocalTime.MIN));
    }

    /**
     * 获取指定时间戳偏移多少天的最后时间
     * 输入格式:yyyyMMdd
     */
    public static Long endTimeOfOffsetDay(int day, long offsetDay) {
        LocalDateTime localDateTime = parseLocalDateTimeToPureDate(day);
        LocalDateTime offsetLocalDateTime = localDateTime.plusDays(offsetDay);
        return localDateTimeToEpochMilli(offsetLocalDateTime.with(LocalTime.MAX));
    }


    /**
     * 两个日期相差天数
     * isReset – 是否重置时间为起始时间
     * 如果isReset为 false相差天数为0。
     * 如果isReset为 true相差天数将被计算为1
     */
    public static long betweenDay(LocalDateTime beginLocalDateTime, LocalDateTime endLocalDateTime, boolean isReset) {
        if (isReset) {
            beginLocalDateTime = beginLocalDateTime.with(LocalTime.MIN);
            endLocalDateTime = endLocalDateTime.with(LocalTime.MIN);
        }
        return ChronoUnit.DAYS.between(beginLocalDateTime, endLocalDateTime);
    }

    /**
     * 两个日期是否是同一天
     */
    public static boolean isSameDay(final LocalDateTime date1, final LocalDateTime date2) {
        return date1 != null && date2 != null && date1.toLocalDate().isEqual(date2.toLocalDate());
    }


    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now();
    }

}
